package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.AppMapServiceManageService;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.service.dto.AppMapServiceManageDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing AppMapServiceManage.
 */
@RestController
@RequestMapping("/api")
public class AppMapServiceManageResource {

    private final Logger log = LoggerFactory.getLogger(AppMapServiceManageResource.class);

    private static final String ENTITY_NAME = "appMapServiceManage";

    private final AppMapServiceManageService appMapServiceManageService;

    public AppMapServiceManageResource(AppMapServiceManageService appMapServiceManageService) {
        this.appMapServiceManageService = appMapServiceManageService;
    }

    /**
     * POST  /app-map-service-manages : Create a new appMapServiceManage.
     *
     * @param appMapServiceManageDTO the appMapServiceManageDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new appMapServiceManageDTO, or with status 400 (Bad Request) if the appMapServiceManage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/app-map-service-manages")
    @Timed
    public ResponseEntity<AppMapServiceManageDTO> createAppMapServiceManage(@RequestBody AppMapServiceManageDTO appMapServiceManageDTO) throws URISyntaxException {
        log.debug("REST request to save AppMapServiceManage : {}", appMapServiceManageDTO);
        if (appMapServiceManageDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new appMapServiceManage cannot already have an ID")).body(null);
        }
        AppMapServiceManageDTO result = appMapServiceManageService.save(appMapServiceManageDTO);
        return ResponseEntity.created(new URI("/api/app-map-service-manages/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /app-map-service-manages : Updates an existing appMapServiceManage.
     *
     * @param appMapServiceManageDTO the appMapServiceManageDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated appMapServiceManageDTO,
     * or with status 400 (Bad Request) if the appMapServiceManageDTO is not valid,
     * or with status 500 (Internal Server Error) if the appMapServiceManageDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/app-map-service-manages")
    @Timed
    public ResponseEntity<AppMapServiceManageDTO> updateAppMapServiceManage(@RequestBody AppMapServiceManageDTO appMapServiceManageDTO) throws URISyntaxException {
        log.debug("REST request to update AppMapServiceManage : {}", appMapServiceManageDTO);
        if (appMapServiceManageDTO.getId() == null) {
            return createAppMapServiceManage(appMapServiceManageDTO);
        }
        AppMapServiceManageDTO result = appMapServiceManageService.save(appMapServiceManageDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, appMapServiceManageDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /app-map-service-manages : get all the appMapServiceManages.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of appMapServiceManages in body
     */
    @GetMapping("/app-map-service-manages")
    @Timed
    public List<AppMapServiceManageDTO> getAllAppMapServiceManages() {
        log.debug("REST request to get all AppMapServiceManages");
        return appMapServiceManageService.findAll();
    }

    /**
     * GET  /app-map-service-manages/:id : get the "id" appMapServiceManage.
     *
     * @param id the id of the appMapServiceManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the appMapServiceManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/app-map-service-manages/{id}")
    @Timed
    public ResponseEntity<AppMapServiceManageDTO> getAppMapServiceManage(@PathVariable Long id) {
        log.debug("REST request to get AppMapServiceManage : {}", id);
        AppMapServiceManageDTO appMapServiceManageDTO = appMapServiceManageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(appMapServiceManageDTO));
    }

    /**
     * DELETE  /app-map-service-manages/:id : delete the "id" appMapServiceManage.
     *
     * @param id the id of the appMapServiceManageDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/app-map-service-manages/{id}")
    @Timed
    public ResponseEntity<Void> deleteAppMapServiceManage(@PathVariable Long id) {
        log.debug("REST request to delete AppMapServiceManage : {}", id);
        appMapServiceManageService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
